package com.pds.web.maintenance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.maintenance.entity.PdsEnvironmentNotice;
import com.pds.web.maintenance.mapper.PdsEnvironmentNoticeMapper;
import com.pds.web.maintenance.service.PdsEnvironmentNoticeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 监管部门通知公告表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-02
 */
@Service("pdsEnvironmentNoticeService")
public class PdsEnvironmentNoticeServiceImpl extends ServiceImpl<PdsEnvironmentNoticeMapper, PdsEnvironmentNotice> implements PdsEnvironmentNoticeService {
    @Resource
    private PdsEnvironmentNoticeMapper pdsEnvironmentNoticeMapper;

    @Override
    public List<PdsEnvironmentNotice> selectList(PdsEnvironmentNotice pdsEnvironmentNotice) {
        QueryWrapper<PdsEnvironmentNotice> wrapper = new QueryWrapper<>();
        String noticeTitle = pdsEnvironmentNotice.getNoticeTitle();
        String noticeContent = pdsEnvironmentNotice.getNoticeContent();
        String noticePeriod = pdsEnvironmentNotice.getNoticePeriod();
        if(!Objects.isNull(noticeTitle) && !noticeTitle.isEmpty()){
            wrapper.like("notice_title",noticeTitle);
        }
        if(!Objects.isNull(noticeContent) && !noticeContent.isEmpty()){
            wrapper.like("notice_content",noticeContent);
        }
        if(!Objects.isNull(noticePeriod) && !noticePeriod.isEmpty()){
            wrapper.eq("notice_period",noticePeriod);
        }
        return pdsEnvironmentNoticeMapper.selectList(wrapper);
    }

    @Override
    public PdsEnvironmentNotice queryById(Long id) {
        return pdsEnvironmentNoticeMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsEnvironmentNotice insert(PdsEnvironmentNotice pdsEnvironmentNotice) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsEnvironmentNotice.setUserId(userId);
        pdsEnvironmentNotice.setCreateBy(nickName);*/
        pdsEnvironmentNotice.setCreateTime(date);
        pdsEnvironmentNotice.setStatus("1");
        pdsEnvironmentNoticeMapper.insert(pdsEnvironmentNotice);
        return pdsEnvironmentNotice;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsEnvironmentNotice pdsEnvironmentNotice) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsEnvironmentNotice.setUpdateBy(nickName);*/
        pdsEnvironmentNotice.setUpdateTime(date);
        return pdsEnvironmentNoticeMapper.updateById(pdsEnvironmentNotice) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsEnvironmentNoticeMapper.deleteById(id) > 0;
    }
}